package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.HouseMapper;
import org.example.main.entity.House;
import org.example.main.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {

    private static final Logger logger = Logger.getLogger(HouseServiceImpl.class);

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public int add(House house) {
        logger.info("Adding new house with title: " + house.getTitle());
        int result = houseMapper.insert(house);
        logger.info("House added with ID: " + house.getHouseId() + ", result: " + result);
        return result;
    }

    @Override
    public int update(House house) {
        logger.info("Updating house with ID: " + house.getHouseId());
        int result = houseMapper.update(house);
        logger.info("House update result: " + result);
        return result;
    }

    @Override
    public int deleteById(long houseId) {
        logger.info("Deleting house by ID: " + houseId);
        int result = houseMapper.deleteById(houseId);
        logger.info("House deletion result: " + result);
        return result;
    }

    @Override
    public House findById(long houseId) {
        logger.info("Finding house by ID: " + houseId);
        House house = houseMapper.findById(houseId);
        logger.debug("Found house: " + house);
        return house;
    }

    @Override
    public List<House> findAll() {
        logger.info("Retrieving all houses");
        List<House> houses = houseMapper.findAll();
        logger.debug("Found " + houses.size() + " houses");
        return houses;
    }

    @Override
    public List<House> findByLandlordId(long landlordId) {
        logger.info("Finding houses by landlord ID: " + landlordId);
        List<House> houses = houseMapper.findByLandlordId(landlordId);
        logger.debug("Found " + houses.size() + " houses for landlord ID: " + landlordId);
        return houses;
    }

    @Override
    public List<House> findByStatus(String status) {
        logger.info("Finding houses by status: " + status);
        List<House> houses = houseMapper.findByStatus(status);
        logger.debug("Found " + houses.size() + " houses with status: " + status);
        return houses;
    }

    @Override
    public List<House> findByTitle(String title) {
        logger.info("Finding houses by title: " + title);
        List<House> houses = houseMapper.findByTitle(title);
        logger.debug("Found " + houses.size() + " houses with title: " + title);
        return houses;
    }

    @Override
    public List<House> findByArea(String area) {
        logger.info("Finding houses by area: " + area);
        List<House> houses = houseMapper.findByArea(area);
        logger.debug("Found " + houses.size() + " houses in area: " + area);
        return houses;
    }

    @Override
    public int updateStatus(long houseId, String status) {
        logger.info("Updating status for house ID: " + houseId + " to status: " + status);
        int result = houseMapper.updateStatus(houseId, status);
        logger.info("Status update result: " + result);
        return result;
    }
}
